package com.pactera.madp.cp.controller.sidebar.marketing;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.cp.api.page.MarketingPage;
import com.pactera.madp.cp.api.vo.marketing.MarketingHotVO;
import com.pactera.madp.cp.common.util.PageUtil;
import com.pactera.madp.cp.service.MarketingHotService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Auther: yuShen.qi
 * @Date: 2021/5/26 10:40
 * @Description:
 */
@RestController
@RequestMapping("/marketinghotwechat")
@Api(value = "marketingHot", tags = "营销素材-分类管理-热榜侧边栏")
public class MarketingHotSideBarController {

    @Resource
    private MarketingHotService marketingHotService;

    /**
     * 统计文章打开次数
     */
    @GetMapping("/open")
    @ApiOperation(value = "统计打开次数")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value="营销素材id",dataType ="String"),
            @ApiImplicitParam(name="type",value="营销素材类型",dataType ="String"),
            @ApiImplicitParam(name="url",value="素材链接",dataType ="String"),
            @ApiImplicitParam(name="wxExternalUserid",value="微信外部联系人id",dataType ="String"),
            @ApiImplicitParam(name="wxUserId",value="微信成员id",dataType ="String"),
            @ApiImplicitParam(name="location",value="侧边栏位置",dataType ="String"),
            @ApiImplicitParam(name="title",value="主题",dataType ="String"),
            @ApiImplicitParam(name="corpId",value="企业id",dataType ="Integer")
    })
    public R countOpen(@RequestParam(name = "materialId") String materialId,
                          @RequestParam(name = "materialType") String materialType,
                          @RequestParam(name = "url") String url,
                          @RequestParam(name = "wxExternalUserid") String wxExternalUserid,
                          @RequestParam(name = "wxUserId") String wxUserId,
                          @RequestParam(name = "location") String location,
                          @RequestParam(name = "title") String title,
                          @RequestParam(name = "corpId") Integer corpId,
                          HttpServletResponse response) throws IOException {
        marketingHotService.countOpen(materialId,materialType,url,wxExternalUserid,wxUserId,location,title,corpId,response);
        return R.ok();
    }

    /**
     * 统计文章发送次数
     *
     * @param id
     * @param type
     * @return
     */
    @GetMapping("/send")
    @ApiOperation(value = "统计发送次数")
    public R countSend(@RequestParam(value = "id") String id,@RequestParam(value = "type") String type,@RequestParam(value = "wxExternalUserid") String wxExternalUserid){
         marketingHotService.countSend(id,type,wxExternalUserid);
         return R.ok();
    }

    /**
     * 得到素材打开次数和发送次数并分页
     *
     * @return
     */
   @GetMapping("/page")
   @ApiOperation(value = "得到素材打开次数和发送次数")
    public R<Page<MarketingHotVO>> getList(MarketingPage page,Integer hotState,String title){
       List<MarketingHotVO> marketingHotVOS = marketingHotService.allListWechat(hotState,title);
       return R.ok(PageUtil.split(marketingHotVOS,page.getPageCurrent(),page.getPageSize()));
   }

    /**
     * 通过类型获取素材的打开和发送次数
     *
     * @param type
     * @param page
     * @return
     */
   @GetMapping("/page_type")
    @ApiOperation(value = "通过类型获取素材的打开和发送次数")
    public R<Page> getListByType(@RequestParam(value = "type") Integer type, @RequestParam(value = "hotState") Integer hotState, MarketingPage page, String title){
       List listByType = marketingHotService.getListByTypeWechat(type,hotState,title);
     return R.ok(PageUtil.split(listByType, page.getPageCurrent(),page.getPageSize()));
   }

   @GetMapping("/sate")
   @ApiOperation(value = "查询当前热榜发布方式")
    public R<Map<String,Integer>> getSate(){
       return R.ok(marketingHotService.hotState());
   }

}
